﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DataType.MessageType;
using MapCalclator;
using MathNet.Numerics.Distributions;

namespace CommunicationEnvironment
{
    public class CommEnv
    {
        private List<int> big_city;
        private List<int> free_model;
        private int big_city_n0;
        private float big_city_decay;
        private int sky_n0;
        private float sky_decay;
        private int free_model_n0;
        private float free_model_decay;
        private float fading_param;
        private float fading_gain;

        public int BigCityN0 => big_city_n0;
        public float BigCityDecay => big_city_decay;
        public int SkyN0 => sky_n0;
        public float SkyDecay => sky_decay;
        public int FreeModelN0 => free_model_n0;
        public float FreeModelDecay => free_model_decay;
        public float FadingParam => fading_param;
        public float FadingGain => fading_gain;

        public CommEnv()
        {
            big_city = new List<int>();
            for (int i = 0; i < 1761; i++)
            {
                big_city.Add(i);
            }
            free_model = new List<int>();
            for (int i = 1800; i < 6960; i++)
            {
                free_model.Add(i);
            }
            big_city_n0 = -200;
            big_city_decay = 0.5f;
            sky_n0 = -180;
            sky_decay = 0.5f;
            free_model_n0 = -160;
            free_model_decay = 0.8f;
            fading_param = 1f;
            fading_gain = (float)new Rayleigh(fading_param).Sample();
        }

        public string calculate_control_pos(int pos, int height, Map map)
        {
            int row = pos / 100;
            int col = pos % 100;
            int height1 = map[row, col].elev + height;
            string mode = "";
            if (height1 <= 40 && big_city.Contains(pos))
            {
                mode = "BigCity";
            }
            else if (height1 <= 40 && free_model.Contains(pos))
            {
                mode = "Freemodel";
            }
            else if (height1 > 40)
            {
                mode = "Sky";
            }
            else
            {
                Trace.TraceWarning("当前参数输入有误！");
            }
            return mode;
        }

        public void update_com_env(ObservationMsg observation)
        {
            if (observation.time.cur_step % 100 == 0)
            {
                big_city_n0 += 1;
                sky_n0 += 1;
                free_model_n0 += 1;
            }
            else if (observation.time.cur_step % 150 == 0)
            {
                big_city_n0 -= 1;
                sky_n0 -= 1;
                free_model_n0 -= 1;
            }
        }
    }
}